{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "131a9f05",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First row:  tensor([1., 1., 1., 1.])\n",
      "First column:  tensor([1., 1., 1., 1.])\n",
      "Last column: tensor([1., 1., 1., 1.])\n",
      "tensor([[1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.]])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "\n",
    "tensor = torch.ones(4, 4)\n",
    "print('First row: ',tensor[0])\n",
    "print('First column: ', tensor[:, 0])\n",
    "print('Last column:', tensor[..., -1])\n",
    "tensor[:,1] = 0\n",
    "print(tensor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4a5ec8ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.]])\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "连接张量您可以torch.cat用来连接沿给定维度的一系列张量。另请参阅torch.stack，另一个加入 op 的张量，与torch.cat.\n",
    "'''\n",
    "t1 = torch.cat([tensor, tensor, tensor], dim=0) #dim=0垂直连接与 dim=1 水平连接\n",
    "print(t1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0ac25e95",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[[[-2.3151, -1.1208, -0.5459,  0.5234,  1.1513],\n",
      "           [-1.1437, -0.2295, -1.9580,  0.0407, -0.7782],\n",
      "           [ 0.1096,  0.1883, -1.3640, -1.1367, -0.1428],\n",
      "           [ 0.0270,  0.2065, -1.4473,  0.5881,  0.5371]],\n",
      "\n",
      "          [[ 0.2833, -0.4914, -0.7673,  1.4445, -0.4197],\n",
      "           [-0.0774, -0.0867, -1.0757,  0.6355, -0.6523],\n",
      "           [ 0.1920, -0.4923,  1.0860,  0.0543, -0.0724],\n",
      "           [-0.5766, -0.2174, -0.6146,  1.3687, -0.8409]],\n",
      "\n",
      "          [[-0.0469, -0.5610, -0.4344, -0.6978, -0.3435],\n",
      "           [-0.2572,  0.2697,  0.7215, -0.3463,  0.5336],\n",
      "           [-0.3685,  0.2059, -1.4596, -0.7423, -1.5498],\n",
      "           [ 0.4313, -0.0050, -0.4268,  0.4307,  0.2409]]],\n",
      "\n",
      "\n",
      "         [[[-0.9707,  1.0855,  0.3076, -0.1378,  0.4481],\n",
      "           [-0.4069,  0.3332,  1.1467, -0.2283,  0.9333],\n",
      "           [ 2.0601,  0.9482,  0.6357, -0.1426, -0.0340],\n",
      "           [-0.2907, -1.2114, -0.3405, -0.4824,  0.1381]],\n",
      "\n",
      "          [[-0.5634, -1.5255,  0.7328, -0.2400,  0.6266],\n",
      "           [ 0.4061, -2.1612,  0.9966,  0.3461,  1.6494],\n",
      "           [-0.6009,  0.7823,  0.2930, -0.1267, -0.0930],\n",
      "           [-0.0112, -0.5688,  0.3965,  0.4901,  0.6095]],\n",
      "\n",
      "          [[-0.2097,  1.3370,  1.9547, -1.7703,  0.5987],\n",
      "           [ 0.9409, -0.0874,  0.1409,  2.3866,  0.8265],\n",
      "           [-0.3667,  0.7204, -0.8050,  0.8304, -1.2996],\n",
      "           [-0.0949,  1.1124, -0.0622, -0.1052,  1.6802]]]]])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "120"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "torch.numel\n",
    "\n",
    "torch.numel(input)->int\n",
    "返回input 张量中的元素个数\n",
    "\n",
    "参数: input (Tensor) – 输入张量\n",
    "'''\n",
    "    a = torch.randn(1,2,3,4,5)\n",
    "print(a)\n",
    "torch.numel(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "706d612b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 0., 0.],\n",
       "        [0., 1., 0.],\n",
       "        [0., 0., 1.]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "torch.eye\n",
    "torch.eye(n, m=None, out=None)\n",
    "返回一个2维张量，对角线位置全1，其它位置全0 #这不就是单位矩阵吗\n",
    "'''\n",
    "torch.eye(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d268bd36",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1  2  3]\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "from_numpy\n",
    "\n",
    "torch.from_numpy(ndarray) → Tensor\n",
    "Numpy桥，将numpy.ndarray 转换为pytorch的 Tensor。 \n",
    "返回的张量tensor和numpy的ndarray共享同一内存空间。修改一个会导致另外一个也被修改。返回的张量不能改变大小。\n",
    "'''\n",
    "a = np.array([1, 2, 3])\n",
    "t = torch.from_numpy(a)\n",
    "t\n",
    "t[0]=-1\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "70fe1b2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 3.0000,  4.7500,  6.5000,  8.2500, 10.0000])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "torch.linspace(start, end, steps=100, out=None) → Tensor\n",
    "返回一个1维张量，包含在区间start 和 end 上均匀间隔的steps个点。 输出1维张量的长度为steps。\n",
    "\n",
    "参数:\n",
    "\n",
    "start (float) – 序列的起始点\n",
    "end (float) – 序列的最终值\n",
    "steps (int) – 在start 和 end间生成的样本数\n",
    "out (Tensor, optional) – 结果张量\n",
    "'''\n",
    "torch.linspace(3, 10, steps=5)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "34166a5a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1., 0., 0.],\n",
      "        [0., 1., 0.],\n",
      "        [0., 0., 1.]])\n",
      "tensor([[1., 0., 0.],\n",
      "        [0., 1., 0.],\n",
      "        [0., 0., 1.]])\n",
      "tensor([[1., 0., 0.],\n",
      "        [0., 1., 0.],\n",
      "        [0., 0., 1.]])\n",
      "tensor([[1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.]])\n",
      "tensor([[1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.]])\n",
      "tensor([[1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.]])\n"
     ]
    }
   ],
   "source": [
    "# This computes the matrix multiplication between two tensors. y1, y2, y3 will have the same value\n",
    "y  = torch.eye(3)\n",
    "print(y)\n",
    "y1 = y @ y.T #tensor.T是转置 T- Transpose\n",
    "print(y)\n",
    "print(y.T)\n",
    "\n",
    "y2 = tensor.matmul(tensor.T)\n",
    "\n",
    "y3 = torch.rand_like(tensor)\n",
    "torch.matmul(tensor, tensor.T, out=y3)\n",
    "\n",
    "\n",
    "# This computes the element-wise product. z1, z2, z3 will have the same value\n",
    "z1 = tensor * tensor\n",
    "print(z1)\n",
    "z2 = tensor.mul(tensor)\n",
    "print(z2)\n",
    "z3 = torch.rand_like(tensor)\n",
    "torch.mul(tensor, tensor, out=z3)\n",
    "print(z3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "2531560a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(12.) <class 'torch.Tensor'>\n",
      "12.0 <class 'float'>\n"
     ]
    }
   ],
   "source": [
    "# 单元素张量如果您有一个单元素张量，例如通过将张量的所有值聚合为一个值，您可以使用以下方法将其转换为 Python 数值item()：\n",
    "agg = tensor.sum()\n",
    "print(agg,type(agg))\n",
    "agg_item = agg.item() #不要小看类型转换，做下游任务可能会救命\n",
    "print(agg_item, type(agg_item))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "6f5f4fd7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.],\n",
      "        [1., 0., 1., 1.]]) \n",
      "\n",
      "tensor([[6., 5., 6., 6.],\n",
      "        [6., 5., 6., 6.],\n",
      "        [6., 5., 6., 6.],\n",
      "        [6., 5., 6., 6.]])\n"
     ]
    }
   ],
   "source": [
    "# 就地操作 将结果存储到操作数中的操作称为就地操作。它们由_后缀表示。例如：x.copy_(y), x.t_(), 将改变x。\n",
    "print(tensor, \"\\n\")\n",
    "tensor.add_(5)\n",
    "print(tensor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2da2d8ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "t: tensor([2., 2., 2., 2., 2.], dtype=torch.float64)\n",
      "n: [2. 2. 2. 2. 2.]\n"
     ]
    }
   ],
   "source": [
    "# NumPy 数组到张量\n",
    "n = np.ones(5)\n",
    "t = torch.from_numpy(n)\n",
    "# NumPy 数组中的变化反映在张量中。\n",
    "\n",
    "np.add(n, 1, out=n)\n",
    "print(f\"t: {t}\")\n",
    "print(f\"n: {n}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aacc1043",
   "metadata": {},
   "outputs": [],
   "source": [
    "#2021/7/22 记录一下"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
